package com.slz3.business.module.configManagement.conFeedback.steward;

import com.slz3.business.frame.enums.AuditFlag;
import com.slz3.business.frame.enums.UsingFlag;
import com.slz3.business.frame.module.Services;
import com.slz3.business.frame.redis.Jedis;
import com.slz3.business.frame.redis.RK;
import com.slz3.business.frame.response.RR;
import com.slz3.business.frame.response.ResInfo;
import com.slz3.business.frame.steward.Steward;
import com.slz3.business.frame.util.ConvertUtil;
import com.slz3.business.frame.util.VE;
import com.slz3.business.module.configManagement.conFeedback.domain.ConFeedback;
import com.slz3.business.module.configManagement.conFeedback.domain.ConFeedbackFactory;
import com.slz3.business.module.memberManagement.memMember.domain.User;
import com.slz3.business.module.systemManagement.sysUser.steward.SysUserConverter;
import org.springframework.data.domain.Page;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

public class ConFeedbackResExecutor {

    protected Mono<ServerResponse> add(ServerRequest req, ConFeedback conFeedback) {
        SysUserConverter.setSaveUpdateUser(req, conFeedback, true);
        ConFeedback conFeedbackDBLatest = Services.conFeedback.save(conFeedback);
        return RR.success(req, ResInfo.builder().code(RR.SUCCESS).domain(conFeedbackDBLatest).build());
    }

    /**
     * known: conFeedbackList is not null
     */
    protected Mono<ServerResponse> delete(ServerRequest req) {
        List<ConFeedback> conFeedbackList = RR.attribute(req, RR.RELAY_SAFE_VALID);
        if (VE.isNotEmpty(conFeedbackList)) {
            List<ConFeedback> conFeedbackListNew = Services.conFeedback.delete(conFeedbackList);
            if (VE.isNotEmpty(conFeedbackListNew) && conFeedbackListNew.size() == conFeedbackList.size()) {
                return RR.success(req, ResInfo.builder().code(RR.SUCCESS).domains(conFeedbackListNew).build());
            } else {
                return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("delete fail").build());
            }
        } else {
            return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("cannot find entity").build());
        }
    }

    protected Mono<ServerResponse> modify(ServerRequest req, ConFeedback source) {
        SysUserConverter.setSaveUpdateUser(req, source, false);
        ConFeedback target = RR.attribute(req, RR.RELAY_SAFE_VALID);
        if (target != null) {
            Steward.merge(source, target);
            ConFeedback conFeedbackDBLatest = Services.conFeedback.modify(target);
            return RR.success(req, ResInfo.builder().code(RR.SUCCESS).domain(conFeedbackDBLatest).build());
        } else {
            return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("cannot find entity").build());
        }
    }

    protected Mono<ServerResponse> list(ServerRequest req) {
        Page<ConFeedback> conFeedbackPage = Services.conFeedback.page(req.queryParams());

        List<ConFeedback> conFeedbacks = conFeedbackPage.getContent();
        if(conFeedbacks != null){
            /**
             *  获取主键ID
             */
            List<String> keyList = new ArrayList<>();
            for (ConFeedback vo: conFeedbacks) {
                keyList.add(RK.USER_INFO.concat(":" + vo.getUserID()));
            }
            List<Object> listByKey = Jedis.db0.getListByKey(keyList);
            // 获取用户信息
            List<User> users = ConvertUtil.objToList(listByKey, User.class);
            // 增加用户姓名
            for (ConFeedback vo: conFeedbacks) {
                Optional<User> optionalUser = users.stream().filter(u -> u.getMemberId().equals(vo.getUserID())).findFirst();
                if(optionalUser.isPresent()){
                    User user = optionalUser.get();
                    String serAccountNo = user.getAccountNo();// 用户账户
                    vo.setUserName(serAccountNo);
                }
            }
        }

        return RR.success(req, ResInfo.builder().code(RR.SUCCESS).theme("page").page(conFeedbackPage).build());
    }

    protected Mono<ServerResponse> info(ServerRequest req) {
        ConFeedback target = RR.attribute(req, RR.RELAY_SAFE_VALID);
        if (target != null) {
            return RR.success(req, ResInfo.builder().code(RR.SUCCESS).domain(target).build());
        } else {
            return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("cannot find entity").build());
        }
    }

    protected Mono<ServerResponse> choice(ServerRequest req) {
        Page<ConFeedback> conFeedbackPage = Services.conFeedback.page(req.queryParams());
        return RR.success(req, ResInfo.builder().code(RR.SUCCESS).theme("page").page(conFeedbackPage).build());
    }

    /**
     * known: conFeedbackList is not null
     */
    protected Mono<ServerResponse> auditSubmit(ServerRequest req) {
        List<ConFeedback> conFeedbackList = RR.attribute(req, RR.RELAY_SAFE_VALID);
        if (VE.isNotEmpty(conFeedbackList)) {
            conFeedbackList.stream().forEach(item -> {
                item.setSerAudit(AuditFlag.WAIT_AUDIT);
                ConFeedbackFactory.initBaseFields(item);
            });
            List<ConFeedback> conFeedbackListNew = Services.conFeedbackTransactional.batchModify(conFeedbackList);
            if (VE.isNotEmpty(conFeedbackListNew) && conFeedbackListNew.size() == conFeedbackList.size()) {
                return RR.success(req, ResInfo.builder().code(RR.SUCCESS).domains(conFeedbackListNew).build());
            } else {
                return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("auditSubmit fail").build());
            }
        } else {
            return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("cannot find entity").build());
        }
    }

    /**
     * known: conFeedbackList is not null
     */
    protected Mono<ServerResponse> audit(ServerRequest req) {
        List<ConFeedback> conFeedbackList = RR.attribute(req, RR.RELAY_SAFE_VALID);
        if (VE.isNotEmpty(conFeedbackList)) {
            conFeedbackList.stream().forEach(item -> {
                item.setSerAudit(AuditFlag.APPROVED);
                ConFeedbackFactory.initBaseFields(item);
            });
            List<ConFeedback> conFeedbackListNew = Services.conFeedbackTransactional.batchModify(conFeedbackList);
            if (VE.isNotEmpty(conFeedbackListNew) && conFeedbackListNew.size() == conFeedbackList.size()) {
                return RR.success(req, ResInfo.builder().code(RR.SUCCESS).domains(conFeedbackListNew).build());
            } else {
                return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("audit fail").build());
            }
        } else {
            return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("cannot find entity").build());
        }
    }

    /**
     * known: conFeedbackList is not null
     */
    protected Mono<ServerResponse> auditReject(ServerRequest req) {
        List<ConFeedback> conFeedbackList = RR.attribute(req, RR.RELAY_SAFE_VALID);
        if (VE.isNotEmpty(conFeedbackList)) {
            conFeedbackList.stream().forEach(item -> {
                item.setSerAudit(AuditFlag.REJECT);
                ConFeedbackFactory.initBaseFields(item);
            });
            List<ConFeedback> conFeedbackListNew = Services.conFeedbackTransactional.batchModify(conFeedbackList);
            if (VE.isNotEmpty(conFeedbackListNew) && conFeedbackListNew.size() == conFeedbackList.size()) {
                return RR.success(req, ResInfo.builder().code(RR.SUCCESS).domains(conFeedbackListNew).build());
            } else {
                return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("auditReject fail").build());
            }
        } else {
            return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("cannot find entity").build());
        }
    }

    /**
     * known: conFeedbackList is not null
     */
    protected Mono<ServerResponse> usingEnable(ServerRequest req) {
        List<ConFeedback> conFeedbackList = RR.attribute(req, RR.RELAY_SAFE_VALID);
        if (VE.isNotEmpty(conFeedbackList)) {
            conFeedbackList.stream().forEach(item -> {
                item.setSerUsing(UsingFlag.ENABLE);
                ConFeedbackFactory.initBaseFields(item);
            });
            List<ConFeedback> conFeedbackListNew = Services.conFeedbackTransactional.batchModify(conFeedbackList);
            if (VE.isNotEmpty(conFeedbackListNew) && conFeedbackListNew.size() == conFeedbackList.size()) {
                return RR.success(req, ResInfo.builder().code(RR.SUCCESS).domains(conFeedbackListNew).build());
            } else {
                return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("auditReject fail").build());
            }
        } else {
            return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("cannot find entity").build());
        }
    }


    /**
     * known: conFeedbackList is not null
     */
    protected Mono<ServerResponse> usingDisable(ServerRequest req) {
        List<ConFeedback> conFeedbackList = RR.attribute(req, RR.RELAY_SAFE_VALID);
        if (VE.isNotEmpty(conFeedbackList)) {
            conFeedbackList.stream().forEach(item -> {
                item.setSerUsing(UsingFlag.DISABLE);
                ConFeedbackFactory.initBaseFields(item);
            });
            List<ConFeedback> conFeedbackListNew = Services.conFeedbackTransactional.batchModify(conFeedbackList);
            if (VE.isNotEmpty(conFeedbackListNew) && conFeedbackListNew.size() == conFeedbackList.size()) {
                return RR.success(req, ResInfo.builder().code(RR.SUCCESS).domains(conFeedbackListNew).build());
            } else {
                return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("usingDisable fail").build());
            }
        } else {
            return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("cannot find entity").build());
        }
    }

}
